package com.um.jdy.mp.report.service.impl;


import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.charge.dao.OrderDao;
import com.um.jdy.common.charge.entity.po.Order;
import com.um.jdy.common.device.dao.StationDao;
import com.um.jdy.common.device.entity.po.Station;
import com.um.jdy.common.platform.entity.po.Merchant;
import com.um.jdy.mp.base.entity.vo.StationItemVO;
import com.um.jdy.mp.report.entity.vo.*;
import com.um.jdy.mp.report.enums.DateType;
import com.um.jdy.mp.report.enums.OrderBy;
import com.um.jdy.mp.report.properties.MpReportProperties;
import com.um.jdy.mp.report.service.DeviceDataService;
import com.um.jdy.mp.user.service.SessionUserService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 运营商设备统计
 *
 * @author 邓南辉
 * 日期 2023/2/28
 * 版权所有 广州优匠科技有限公司
 */
@Service
public class DeviceDataServiceImpl extends BaseEntityServiceImpl<OrderDao, Order> implements DeviceDataService {


    @Autowired
    MpReportProperties hshReportProperties;

    @Autowired
    SessionUserService sessionUserService;

    @Autowired
    StationDao stationDao;

    @Autowired
    OrderDao orderDao;

    private Integer allDaySeconds(DateType data_type) {
        Integer days = 1;
        if (data_type == DateType.Week) {
            days = 7;
        } else if (data_type == DateType.Month) {
            days = 30;
        } else if (data_type == DateType.ThreeMonth) {
            days = 90;
        } else if (data_type == DateType.HalfOfYears) {
            days = 180;
        } else if (data_type == DateType.Years) {
            days = 365;
        }
        return days * 24 * 3600;
    }

    private String getStartDate(DateType data_type) {
        Date now = new Date();
        if (data_type == DateType.Week) {
            now.setDate(now.getDate() - 6);
        } else if (data_type == DateType.Month) {
            now.setDate(now.getDate() - 29);
        } else if (data_type == DateType.ThreeMonth) {
            now.setDate(now.getDate() - 89);
        } else if (data_type == DateType.HalfOfYears) {
            now.setDate(now.getDate() - 179);
        } else if (data_type == DateType.Years) {
            now.setDate(now.getDate() - 364);
        }
        return DateUtil.format(now, "yyyy-MM-dd") + " 00:00:00";
    }

    private String getEndDate() {
        return DateUtil.format(new Date(), "yyyy-MM-dd") + " 23:59:59";
    }

    /**
     * 查询站场列表
     *
     * @param name
     * @return
     */
    public Page<StationItemVO> getStationPage(int pageNo, int pageSize, String name) {

        Page<Station> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        page = stationDao.queryPage(page, new String[]{"name", "platform_merchant_id"}, new Object[]{StringUtil.getQueryParam(name),sessionUserService.getTokenUserId()},
                new String[]{"create_time"}, new String[]{"desc"});

        List<StationItemVO> stationList = UMBeanUtils.copyBeanList(page.getResult(),StationItemVO.class);

        Page<StationItemVO> returnPage = new Page<>();

        returnPage.setPageNo(page.getPageNo());
        returnPage.setPageSize(page.getPageSize());
        returnPage.setResult(stationList);
        returnPage.setTotalCount(page.getTotalCount());
        return returnPage;
    }


    /**
     * 查询累计数据
     *
     * @return
     */
    public DeviceSummaryVO getDeviceSummary() {
        String endDate = getEndDate();
        DeviceSummaryVO deviceSummaryVO;

        Date early = DateUtil.parse(hshReportProperties.getBeginDate());
        Merchant merchant = sessionUserService.getTokenUser();
        early = merchant.getCreateTime();

        Integer allDaySeconds = com.um.springboot.starter.utils.DateUtil.daysBetween(early, new Date()) * 24 * 3600;
        deviceSummaryVO = orderDao.getCMSViewModelBySqlName("get_partners_device_summary", DeviceSummaryVO.class,
                new String[]{sessionUserService.getUserIdName()}, new Object[]{sessionUserService.getTokenUserId()},
                new ArrayList<>(), new Object[]{allDaySeconds, endDate}, false);

        deviceSummaryVO.setEndDate(new Date());
        return deviceSummaryVO;

    }

    /**
     * 设备使用率排名
     *
     * @param pageNo
     * @param pageSize
     * @param data_type
     * @param stationId
     * @param orderBy
     * @return
     */
    public Page<DeviceDataItemVO> getDeviceRatePage(int pageNo, int pageSize, DateType data_type, String stationId, OrderBy orderBy) {

        Page<DeviceDataItemVO> page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        Integer allDaySeconds = allDaySeconds(data_type);
        String startDate = getStartDate(data_type);
        String endDate = getEndDate();

        String sqlName = "get_partners_device_rate_page";
        String sqlCountName = "get_partners_device_rate_count";
        return getCMSViewModelPageBySqlName(page, sqlName, sqlCountName, DeviceDataItemVO.class, new String[]{"p." + sessionUserService.getUserIdName()}, new Object[]{sessionUserService.getTokenUserId()},
                new ArrayList<>(), new String[]{"radio"}, new String[]{orderBy.name()}, new Object[]{allDaySeconds, startDate, endDate, StringUtil.getTrim(stationId, "%"), StringUtil.getTrim(stationId, "%")}, false);

    }

    /**
     * 站场设备使用率排名
     *
     * @param pageNo
     * @param pageSize
     * @param data_type
     * @param orderBy
     * @return
     */
    public Page<StationDataItemVO> getStationDeviceRatePage(int pageNo, int pageSize, DateType data_type,OrderBy orderBy) {
        Page<StationDataItemVO> page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer allDaySeconds = allDaySeconds(data_type);
        String startDate = getStartDate(data_type);
        String endDate = getEndDate();

        String sqlName = "get_partners_station_rate_page";
        String sqlCountName = "get_partners_station_rate_count";
        return getCMSViewModelPageBySqlName(page, sqlName, sqlCountName, StationDataItemVO.class, new String[]{"s." + sessionUserService.getUserIdName()}, new Object[]{sessionUserService.getTokenUserId()},
                new ArrayList<>(), new String[]{"radio"}, new String[]{orderBy.name()}, new Object[]{allDaySeconds, startDate, endDate}, false);
    }

    /**
     * 设备上下线统计
     *
     * @param pageNo
     * @param pageSize
     * @param data_type
     * @param stationId
     * @return
     */
    public Page<DeviceStatusItemVO> getDeviceOfflinePage(int pageNo, int pageSize, DateType data_type, String stationId) {
        Page<DeviceStatusItemVO> page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        String startDate = getStartDate(data_type);
        String endDate = getEndDate();

        String sqlName = "get_partners_device_offline_page";
        String sqlCountName = "get_partners_device_offline_count";
        return getCMSViewModelPageBySqlName(page, sqlName, sqlCountName, DeviceStatusItemVO.class, new String[]{"s." + sessionUserService.getUserIdName()}, new Object[]{sessionUserService.getTokenUserId()},
                new ArrayList<>(), new String[]{}, new String[]{}, new Object[]{startDate, endDate, StringUtil.getTrim(stationId, "%")}, false);

    }

    /**
     * 未产生订单设备
     *
     * @param pageNo
     * @param pageSize
     * @param data_type
     * @param stationId
     * @return
     */
    public Page<DeviceDataItemVO> getDeviceNoOrderPage(int pageNo, int pageSize, DateType data_type, String stationId) {
        Page<DeviceDataItemVO> page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        String startDate = getStartDate(data_type);
        String endDate = getEndDate();

        String sqlName = "get_partners_notorder_device_page";
        String sqlCountName = "get_partners_notorder_device_count";
        return getCMSViewModelPageBySqlName(page, sqlName, sqlCountName, DeviceDataItemVO.class, new String[]{"s." + sessionUserService.getUserIdName()}, new Object[]{sessionUserService.getTokenUserId()},
                new ArrayList<>(), new String[]{}, new String[]{}, new Object[]{startDate, endDate, StringUtil.getTrim(stationId, "%"), StringUtil.getTrim(stationId, "%")}, false);

    }
}
